package com.leo.manage_system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.Advertise;
import com.leo.manage_system.entity.Res;
import com.leo.manage_system.entity.vo.ResQueryVo;
import com.leo.manage_system.service.AdvertiseService;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 广告 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@RestController
@RequestMapping("/service_manage/GG")
@Api(tags = "广告管理")
@CrossOrigin
public class AdvertiseController {
    final AdvertiseService advertiseService;
    JwtUtils jwtUtils = new JwtUtils();
    @Autowired
    public AdvertiseController(AdvertiseService advertiseService){
        this.advertiseService=advertiseService;
    }

    @ApiOperation(value = "添加广告")
    @PostMapping("addGG")
    public R addAdmin(@ApiParam(name = "token", value = "token", required = true)
                          @RequestParam(value="token") String token,
                      @ApiParam(name = "advertise", value = "广告",required = true) @RequestBody Advertise advertise)
    {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = advertiseService.save(advertise);
            if (!code) {
                return R.error().message("添加失败");
            }
            return R.ok().message("添加成功");
        }
    }
    @ApiOperation(value = "删除广告")
    @DeleteMapping("deleteGG")
    public R deleteAdmin(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "id", value = "广告ID", required = true)
            @RequestParam String id){

        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = advertiseService.deleteAdvertise(id);
            if(!code) {
                return R.error().message("删除失败");
            }
            return R.ok().message("删除成功");
        }
    }
    @ApiOperation("编辑广告")
    @PostMapping("/modifyGG")
    public R modifyRes(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam("广告") @RequestBody Advertise advertise) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = advertiseService.updateById(advertise);
            if(!code) {
                return R.error().message("编辑失败");
            }
            return R.ok().message("编辑成功");
        }
    }
    @ApiOperation(value = "分页广告列表")
    @PostMapping("GGPageQuery/{page}/{limit}")
    public R getAdvertisePageQuery(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit
    ){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            Page<Advertise> pageParam = new Page<>(page, limit);
            return R.ok().data(advertiseService.getAdvertisePageQuery(pageParam));
        }
    }
    @ApiOperation(value = "广告URL")
    @PostMapping("getGGURL/")
    public R getAdvertise(

    ){
        List <String> list = new ArrayList<>();

        return R.ok().data("List",advertiseService.getAdvertise());

    }


}

